﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Adex;
using Telerik.Web.UI;
using Ionic.Zip;
using System.IO;


namespace MediaMonitor.Users{
public partial class Orders : Adex.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable dt = VOrder.Query().SetSelectList("*, CONVERT(VARCHAR, DateAdded, 101) AS OrderDate").WHERE("SiteUserID", MediaMonitor.Common.AppUser.CurrUserID).ORDER_BY("orderid DESC").ExecuteDataSet().Tables[0];
        radGrid.DataSource = dt;
        
    }

    

    protected void radGrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e){

        if (e.CommandName == RadGrid.ExportToPdfCommandName)
        {
            radGrid.MasterTableView.HierarchyDefaultExpanded = true;
            radGrid.MasterTableView.Rebind();

            radGrid.ExportSettings.OpenInNewWindow = true;
            radGrid.ExportSettings.ExportOnlyData = true;
            radGrid.MasterTableView.HierarchyDefaultExpanded = true;
            radGrid.MasterTableView.ExportToPdf();
        }

        else if (e.CommandName == "DownloadAll")
        {
            GridDataItem item = e.Item as GridDataItem;
            Int32 OrderID = Convert.ToInt32(item.GetDataKeyValue("OrderID"));
            if (OrderID > 0)
            {
                DataTable dtOrder = VOrderDetail.Query().SetSelectList("*").WHERE("OrderID", OrderID).ExecuteDataSet().Tables[0];
                if (dtOrder.Rows.Count > 0)
                {
                    #region[Zip Format]
                    Response.Clear();
                    // no buffering - allows large zip files to download as they are zipped
                    Response.BufferOutput = false;
                    String filePath = String.Empty;
                  
                    String downloadFileName = OrderID+"_Order.zip";
                    Response.ContentType = "application/zip";
                    Response.AddHeader("content-disposition", "filename=" + downloadFileName);

                    using (ZipFile zip = new ZipFile())
                    {
                        foreach (DataRow row in dtOrder.Rows)
                        {
                            String filePathCheck = String.Empty;
                            
                            if (row["MediaTypeCode"].ToString() == "1") //TV
                            {
                                 filePath = row["ShortVideoFile"].ToString();
                                 filePathCheck = Server.MapPath("../videos/" + filePath);
                            }

                            else if (row["MediaTypeCode"].ToString() == "3") //RADIO
                            {
                                filePath = row["RadioFile"].ToString();
                                filePathCheck = Server.MapPath("../radio/" + filePath);
                            }

                            else if (row["MediaTypeCode"].ToString() == "5") //Cable
                            {
                                filePath = row["CableFile"].ToString();
                                filePathCheck = Server.MapPath("../radio/" + filePath);
                            }

                            else if (row["MediaTypeCode"].ToString() == "6") // PRESS
                            {
                                filePath = row["PressFile"].ToString();
                                filePathCheck = Server.MapPath("../Press/" + filePath);
                            }

                            else if (row["MediaTypeCode"].ToString() == "7") // OUTDOOR
                            {
                                filePath = row["OutDoorFile"].ToString(); 
                                filePathCheck = Server.MapPath("../outdoor/" + filePath);
                            }
                            else if (row["MediaTypeCode"].ToString() == "9") // INTERNET
                            {
                                filePath = row["InternetFile"].ToString();
                                filePathCheck = Server.MapPath("../internet/" + filePath);
                            }
                           
                            if (filePath!="")
                            {
                                 if (File.Exists(filePathCheck))
                                 {
                                     try
                                     {
                                         zip.AddFile(filePathCheck, OrderID + "_Order");
                                     }
                                     catch { }
                                 }
                            }
                        }
                        zip.Save(Response.OutputStream);
                    }
                    Response.Close();
                    Response.End();
                   

                    #endregion
                }
            }


        }


    }
    protected void radGrid_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem gridItem = e.Item as GridDataItem;
            try
            {
                TableCell buttonTc = gridItem["selectButton"];
                ControlCollection controls = buttonTc.Controls;
                foreach (Control button in controls)
                {
                    ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(button);
                }
            }
            catch (Exception) { return; }
        }
    }
   


    protected void btnPrint_Clicked(object sender, EventArgs e) {

        //if (radGrid.SelectedItems.Count != 0)
        //{
        //    foreach (GridDataItem item in radGrid.MasterTableView.Items)
        //    {
        //        if (!item.Selected)
        //            item.Visible = false;
        //    }
        //}

        radGrid.ExportSettings.OpenInNewWindow = true;
        radGrid.ExportSettings.ExportOnlyData = true;
        radGrid.MasterTableView.HierarchyDefaultExpanded = true;
        radGrid.MasterTableView.ExportToPdf();
    
    }
}
}